<template>
  <div class="common-layout">
    <el-container>
      <!-- 头部区域 -->
      <el-header class="headers">
        <img src="/OIG2.jpg" height="50px" width="50px">
        <div class="title">
          小额贷款管理系统
        </div>
        <div class="menu">
          <el-menu
              default-active="1"
              class="el-menu-horizontal"
              mode="horizontal"
              router
          >
            <el-menu-item index="/user_Layout/homeUser"><el-icon class="el-icon--left" ><HomeFilled /></el-icon>首页</el-menu-item>
            <el-menu-item index="/user_Layout/user_Personal_Information"><el-icon class="el-icon--left"><Avatar /></el-icon>个人中心</el-menu-item>
            <el-sub-menu index="1">
              <template #title>
                <el-icon><Money /></el-icon>
                <span>贷款服务</span>
              </template>
              <el-menu-item index="/user_Layout/user_LoanType"><el-icon class="el-icon--left"><Goods /></el-icon>贷款产品类型及申请</el-menu-item>
              <el-menu-item index="/user_Layout/user_Loan_Application"><el-icon class="el-icon--left"><DocumentChecked /></el-icon>贷款申请通过情况</el-menu-item>
            </el-sub-menu>
            <el-menu-item index="/user_Layout/user_Repay"><el-icon><List /></el-icon>还款情况</el-menu-item>
          </el-menu>
        </div>
        <div class="welcome">
          <el-dropdown>
            <span class="el-dropdown-link">
              欢迎：<span v-if="user != null">{{ user.fullName }}</span>
              <el-icon class="el-icon--right">
                <arrow-down />
              </el-icon>
            </span>
            &nbsp;&nbsp;&nbsp;&nbsp;
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="alterPassword">修改密码</el-dropdown-item>
                <el-dropdown-item @click="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      <!-- 主要内容区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>

    <el-dialog :before-close="resetForm"  v-model="info.alterPasswordDialog" title="修改密码" width="500">
      <el-form :model="info.alterPasswordForm" :rules="rules" ref="ruleFormRef"  label-width="auto" style="max-width: 600px">
        <el-form-item label="原密码" prop="password" >
          <el-input v-model="info.alterPasswordForm.password" type="password" placeholder="请输入原密码"
                    show-password autocomplete="off"/>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword" >
          <el-input v-model="info.alterPasswordForm.newPassword" type="password" placeholder="请输入新密码"
                    show-password autocomplete="off"/>
        </el-form-item>
        <el-form-item label="新密码" prop="newPasswordConfirm" >
          <el-input v-model="info.alterPasswordForm.newPasswordConfirm" type="password" placeholder="请再次输入新密码"
                    show-password autocomplete="off"/>
        </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="resetForm">取消</el-button>
          <el-button type="primary" @click="submitNewPassword">
            确认
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router';
import {reactive, ref} from 'vue';
import { ElMessage } from 'element-plus';
import {ArrowDown, Avatar, Document, HomeFilled, List} from "@element-plus/icons-vue";
import xx from "@/axios/xx";



const user = JSON.parse(sessionStorage.getItem('user'));
const info = reactive({
  alterPasswordDialog:false,
  alterPasswordForm:{
  },
});
const router = useRouter();
const ruleFormRef = ref({
});
const validatePass = (rule, value, callback) => {

  if (info.alterPasswordForm.newPasswordConfirm !== '') {
    if (!ruleFormRef.value) {
      callback(new Error('表单引用不存在'));
      return;
    }
    ruleFormRef.value.validateField('newPasswordConfirm').catch(() => {
      callback(new Error('验证新密码确认字段时出错'));
    });
  }
  callback();
}

const validatePass2 = (rule, value, callback) => {
  if(value !== info.alterPasswordForm.newPassword) {
    callback(new Error('两次输入的密码不一致！'));
  }
  callback();
}

const rules = reactive({
  password: [
    { required: true, message: '请输入原密码', trigger: 'blur' }
  ],
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { validator: validatePass, trigger: 'blur' }
  ],
  newPasswordConfirm: [
    { required: true, message: '请再次输入新密码', trigger: 'blur' },
    { validator: validatePass2, trigger: 'blur' }
  ]
});



const resetForm = () => {
  if (!ruleFormRef) return
  ruleFormRef.value.resetFields()
  info.alterPasswordDialog = false
}

const alterPassword = () => {
  info.alterPasswordDialog = true
}

const submitNewPassword = () => {
  if (!ruleFormRef) return
  ruleFormRef.value.validate((valid) => {
    if (valid) {

      info.alterPasswordForm.userId = user.userId
      xx.put("/userInfo/alterPassword", info.alterPasswordForm).then(res => {
        console.log(res)
        if (res.code === 200) {
          ElMessage({
            message: res.message+'请重新登录',
            type: 'success'
          })
          logout()
        } else {
          ElMessage.error(res.message)

        }
      })
      info.alterPasswordDialog = false
      info.alterPasswordForm = {}
    }
  })
}
const logout = () => {
  sessionStorage.removeItem('user');
  router.push('/');
};
</script>

<style scoped>
.headers {
  background-color: #ffffff;
  color: #12083f;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.title {
  font-size: 24px;
}

.menu {
  flex: 1;
}

.welcome {
  display: flex;
  align-items: center;
}

.welcome .el-dropdown {
  color: #12083f;
  cursor: pointer;
}

/* 自定义菜单样式 */
.el-menu-horizontal {
  background-color: transparent;
  border: none;
}

.el-menu-horizontal .el-menu-item,
.el-menu-horizontal .el-submenu {
  margin-right: 20px;
}

.el-menu-horizontal .el-submenu .el-menu {
  background-color: transparent;
  border: none;
}
</style>





